<template>
  <div class="cart-wrapper">
    <div class="content" @click="toggleList()">
      <!--购物车左侧-->
      <div class="content-left">
        <div class="logo-wrapper">
          <div class="logo" :class="{'highlight': totalCount > 0}">
            <i class=" icon-shopping_cart highlight"></i>
          </div>
          <div class="num" v-show="totalCount > 0">
            {{totalCount}}
          </div>
        </div>
        <span class="price" :class="{'highlight': totalPrice > 0}">￥{{totalPrice}}</span>
        <span class="desc">另需配送费{{deliveryPrice}}元</span>
      </div>
      <!--购物车右侧-->
      <div class="content-right" :class="payClass" @click.stop="pay()">
        <span>{{payDesc}}</span>
      </div>
    </div>

    <!--购物车详情-->
    <div class="shop-cart-list" v-show="listShow">
      <div class="list-header">
        <span class="name">购物车</span>
        <span class="remove" @click="remove()">清空</span>
      </div>
      <div class="list-content" ref="listContent">
        <ul>
          <li v-for="item in selectFoods" class="food">
            <span class="name">{{item.name}}</span>
            <div class="price">
              <span>￥{{item.price * item.count}}</span>
            </div>
            <div class="cart-control-wrapper">
              <v-cart-control :food="item"></v-cart-control>
            </div>
          </li>
        </ul>
      </div>
    </div>

    <!--遮罩层-->
    <div class="list-mask" v-show="listShow" @click="toggleList()"></div>
  </div>
</template>

<script>
  import CartControl from '../CartControl/CartControl';
  import BScroll from 'better-scroll';

  export default {
    components: {
      'v-cart-control': CartControl
    },
    data () {
      return {
        fold: true
      }
    },
    props: {
      deliveryPrice: {
        type: Number,
        default: 0
      },
      minPrice: {
        type: Number,
        default: 0
      },
      selectFoods: {
        type: Array,
        default: function() {
          return []
        }
      }
    },
    methods: {
      toggleList() {
        if (!this.totalCount) {
          return;
        }
        this.fold = !this.fold;
      },
      remove() {
        this.selectFoods.forEach((food) => {
          food.count = 0;
        })
      },
      pay() {
        if (this.totalPrice < this.minPrice) {
          return;
        }else {
          window.alert(`需支付￥${this.totalPrice}`)
        }
      }
    },
    computed: {
      totalPrice: function () {
        let total = 0;
        this.selectFoods.forEach((food) => {
          total += food.price * food.count;
        });
        return total;
      },
      totalCount: function () {
        let count = 0;
        this.selectFoods.forEach((food) => {
          count += food.count;
        });
        return count;
      },
      payDesc: function () {
        if (this.totalPrice === 0) {
          return `￥${this.minPrice}元起送`;
        }else if (this.totalPrice < this.minPrice) {
          let dif = this.minPrice - this.totalPrice;
          return `还差￥${dif}起送`;
        }else {
          return '去结算';
        }
      },
      payClass: function () {
        if (this.totalPrice < this.minPrice) {
          return 'not-enough';
        } else if (this.totalPrice >= this.minPrice) {
          return 'enough';
        }
      },
      listShow: function () {
        if (!this.totalCount) {
          this.fold = true;
          return false;
        }
        let isShow = !this.fold;
        if (isShow) {
          this.$nextTick(() => {
            if (!this.$scroll) {
              this.scroll = new BScroll(this.$refs.listContent, {
                click: true
              });
            }else {
              this.scroll.refresh();
            }
          });
        }

        return isShow;
      }
    },
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
  @import "../../common/css/icon.less";
  @import "../../common/css/minxin.less";

  .cart-wrapper {
    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 10;
    width: 100%;
    height: 48px;
    .content {
      display: flex;
      font-size: 0;
      background: #141d27;
      .content-left {
        flex: 1;
        .logo-wrapper {
          position: relative;
          display: inline-block;
          top: -10px;
          margin: 0 12px;
          padding: 6px;
          width: 56px;
          height: 56px;
          box-sizing: border-box;
          vertical-align: top;
          border-radius: 50%;
          background: #141d27;
          .logo {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            text-align: center;
            background: #2b343c;
            &.highlight {
              background: rgb(0, 160, 220);
            }
            .icon-shopping_cart {
              line-height: 44px;
              font-size: 24px;
              color: #80858a;
              &.highlight {
                color: #fff;
              }
            }
          }
          .num {
            position: absolute;
            top: 0;
            right: 0;
            width: 24px;
            height: 16px;
            line-height: 16px;
            font-size: 9px;
            font-weight: 700;
            text-align: center;
            color: #fff;
            background: rgb(240, 20, 20);
            border-radius: 6px;
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, .4);
          }
        }
        span {
          display: inline-block;
          vertical-align: top;
          font-size: 16px;
          line-height: 24px;
          color: rgba(255, 255, 255, .4);
        }
        .price {
          margin-top: 12px;
          padding-right: 12px;
          border-right: 1px solid rgba(255, 255, 255, .1);
          font-weight: 700;
          &.highlight {
            color: #fff;
          }
        }
        .desc {
          margin: 12px 0 0 12px;
        }
      }
      .content-right {
        flex: 0 0 105px;
        width: 105px;
        text-align: center;
        color: rgba(255, 255, 255, .4);
        background: #2b333b;
        &.not-enough {
          background: #2b333b;
        }
        &.enough {
          color: #fff;
          background: #00b43c;
        }
        span {
          height: 48px;
          line-height: 48px;
          font-size: 12px;
          font-weight: 700;

        }
      }
    }
    .shop-cart-list {
      position: absolute;
      bottom: 48px;
      left: 0;
      z-index: -1;
      width: 100%;
      .list-header {
        height: 40px;
        line-height: 40px;
        padding: 0 18px;
        background: #f3f5f7;
        border-bottom: 1px solid rgba(7, 17, 27, .1);
        .name {
          float: left;
          font-size: 14px;
          color: rgb(7, 17, 27);
        }
        .remove {
          float: right;
          font-size: 12px;
          color: rgb(0,160, 220);
        }
      }
      .list-content {
        overflow: hidden;
        padding: 0 18px 30px;
        max-height: 217px;
        background: #fff;
        .food {
          position: relative;
          padding: 12px 0;
          box-sizing: border-box;
          .border-1px(rgba(7, 17, 27, .1));
          .name {
            line-height: 24px;
            font-size: 14px;
            color: rgb(7, 17, 27);
          }
          .price {
            position: absolute;
            right: 90px;
            bottom: 12px;
            line-height: 24px;
            color: rgb(240, 20, 20);
            font-size: 14px;
            font-weight: 700;
          }
          .cart-control-wrapper {
            position: absolute;
            right: 0;
            bottom: 6px;
          }
        }
      }
    }
    .list-mask {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: -2;
      background: rgba(7, 17, 27, .6);
      backdrop-filter: blur(10px);
    }
  }
</style>
